package com.leetcode.algorithm.y22.m09.w3;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 938. 二叉搜索树的范围和
 * 
 * https://leetcode.cn/problems/range-sum-of-bst/
 * 
 * @author jie.deng
 *
 */
class Question0938Solution01 {

	public int rangeSumBST(TreeNode root, int low, int high) {
		if (root == null) {
			return 0;
		}
		// 中序遍历
		int left = 0;
		if (root.val >= low) {
			left = rangeSumBST(root.left, low, high);
		}
		int mid = 0;
		if (root.val >= low && root.val <= high) {
			mid = root.val;
		}
		int right = 0;
		if (root.val <= high) {
			right = rangeSumBST(root.right, low, high);
		}
		return left + mid + right;
	}

}